var username=localStorage.getItem("username");
var token=localStorage.getItem("token");
var userid=localStorage.getItem("userid");
$(function(){
	var paramMap = params();
	var id = paramMap['id'];
	$("#supplyID").val(id);
	$("#modifyUser").val(username);
	$("#sub").click(function(){
		if(!$("form").valid()){
			return "";
		}
		var supplyId = id;
		$.ajax({
			type:'post',
			data:$("form").serialize(),
			url:server_url+'/rest/supplyManage/updateSupply.do?token='+token+'&ran='+Math.random(),
			success:function(){
				easyDialog.open({
					container:{
						header:'温馨提示',
						content:"提交成功",
						yesFn:function(){
							  var url=server_url+'/supplyManage/list.do';
								tl_config.last_url=url;
								tl_config.loadToWorkArea(url);
						  }
					}
				});
			},
			error:function(data){
				easyDialog.open({
					container:{
						header:'温馨提示',
						content:data.msg
					},
					autoClose : 2000
				});
			}
		});
	});
	$("#backList").click(function(){
		var url=server_url+'/supplyManage/list.do';
		tl_config.last_url=url;
		tl_config.loadToWorkArea(url);
	});
	
	$.ajax({
		type : 'GET',
		contentType : 'application/json',
		async : false,
		data : {
			supplyid : id,
			token:token
		},
		url : server_url+'/rest/supplyManage/getSupplyWithArea.do?ran='+Math.random(),
		dataType : 'json',
		success : function(data) {
			if (data.retCode == "0000") {
				pasteData(data.data);
			} else{
				easyDialog.open({
					container:{
						header:'温馨提示',
						content:data.retMsg
					},
					autoClose : 2000
				});
			}
		}
	});
	
	$("input[type=file]").change(function(){
		ajaxfileuploadUtil(this,"supply");
	});
	
	$("#province").change(function(){
		areaParentChange("city", $(this).val(),"county");
	});
	$("#city").change(function(){
		areaParentChange("county", $(this).val());
	});
	$("#accountProvince").change(function(){
		areaParentChange("accountCity", $(this).val());
	});
	
	//表单验证
	var validate = $("form").validate({		   
			rules:{
				resMobile:{
					  required:true,
					  stringMaxLength:30
				},
				scale:{
					   required:true
				 },
				avgYield:{
					   required:true
				},
				bankName:{
					   required:true,
					   stringMaxLength:30
				},
				bank:{
					   required:true,
					   stringMaxLength:30
				},
				bankCard:{
					   required:true,
					   stringMaxLength:30
				}
			},
			messages:{
				resMobile:{
					required:"该值不能为空！",
					stringMaxLength:"最多能输入30个字符！"
				},
				scale:{
					required:"该值不能为空！"
				 },
				avgYield:{
					required:"该值不能为空！"
				},
				bankName:{
					required:"该值不能为空！",
	                stringMaxLength:"最多能输入30个字符！"
				},
				bank:{
					required:"该值不能为空！",
	                stringMaxLength:"最多能输入30个字符！"
				},
				bankCard:{
					required:"该值不能为空！",
	                stringMaxLength:"最多能输入30个字符！"
				}
			},
			errorPlacement: function(error, element) {
				if ( element.is(":radio") )
					error.appendTo ( element.parent() );
				else if ( element.is(":checkbox") )
					error.appendTo ( element.parent() );
				else if ( element.is("input[name=captcha]") )
					error.appendTo ( element.parent() );
				else
					error.insertAfter(element);
			},
			validClass : 'rightValidate',
		    success: function(label) {
			   label.html("&nbsp;").text(" ") ;
		    }
	 });
	
});

function isEmpty(data){
	if(data==null||data==undefined||data.length==0){
		return true;
	}else{
		return false;
	}
}
function pasteData(data){
	console.log(data);
	var supply = data.supply;
	var provinces = data.provinces;
	var citys = data.citys;
	var countrys = data.countrys;
	var accountCitys = data.accountCitys;
	
	$("#supplyType").text('链牛卖家');//TODO
	
	mapIfExist(function(dt){
		$("<option value="+dt.dicValue+">"+dt.dicName+"</option>").appendTo("#pedigreeYearS");
	}, data.pedigreeYearType);
	mapIfExist(function(dt){
		$("<option value="+dt.dicValue+">"+dt.dicName+"</option>").appendTo("#scale");
	}, data.scaleType);
	mapIfExist(function(dt){
		$("<option value="+dt.dicValue+">"+dt.dicName+"</option>").appendTo("#avgYield");
	}, data.avgYieldType);

	$("#pedigreeYearS").val(supply.pedigreeYear);
	$("#scale").val(supply.scale);
	$("#avgYield").val(supply.avgYield);
	$("#supplyName").text(supply.supplyName);
	$("#resMobile").text(supply.resMobile);
	$("[name=resMobile]").val(supply.resMobile);
	$("#addTime").text(supply.addTime);
	mapIfExist(function(province){
		$("<option value="+province.areaId+">"+province.areaName+"</option>").appendTo("#province");
	},provinces);
	mapIfExist(function(city){
		$("<option value="+city.areaId+">"+city.areaName+"</option>").appendTo("#city");
	},citys);
	mapIfExist(function(country){
		$("<option value="+country.areaId+">"+country.areaName+"</option>").appendTo("#county");
	},countrys);
	$("#province").val(supply.province);
	$("#city").val(supply.city);
	$("#country").val(supply.county);
	$("#address").val(supply.address);
	$("#bank").val(supply.bank);
	$("#bankName").val(supply.bankName);
	if(supply.isPedigree==1){
		$("#isPedigree").attr("checked", true);
		$("#pedigreeYearS").css({"display":"block"});
		$("#pedigreeYear").val("1");
	}
	else
	{
		$("#isPedigree").attr("checked", false);
		$("#pedigreeYearS").css({"display":"none"});
		$("#pedigreeYear").val("0");
	}

	$("#bankCard").val(supply.bankCard);
	if(supply.isRegister==1){
		$("#isRegister").attr("checked", true);
	}
	else
	{
		$("#isRegister").attr("checked", false);
	}
	if(supply.isVoice==1){
		$("#isVoice").attr("checked", true);
	}
	else
	{
		$("#isVoice").attr("checked", false);
	}
	$("input[name=pedigreeImg]").val(supply.pedigreeImg);
	$("#pedigreeImg").attr("src",supply.pedigreeImg);
	$("#remark").val(supply.remark);

}

$("#isPedigree").change(function() {
	if($('#isPedigree').is(':checked')) {
		$("#pedigreeYearS").css({"display":"block"});
		$("#pedigreeYear ").val("1");
	}
	else
	{
		$("#pedigreeYearS").css({"display":"none"});
		$("#pedigreeYear ").val("0");
	}
});

